<template>
  <div class="starwars-login-wrap">
    <!-- 太空背景 -->
    <div class="space-bg">
      <div class="stars-layer stars-layer-1"></div>
      <div class="stars-layer stars-layer-2"></div>
      <div class="stars-layer stars-layer-3"></div>
      <div class="star-field"></div>
      <div class="planets">
        <div class="planet planet-1"></div>
        <div class="planet planet-2"></div>
        <div class="planet planet-3"></div>
      </div>
      <div class="ship-trails">
        <div class="trail trail-1"></div>
        <div class="trail trail-2"></div>
        <div class="trail trail-3"></div>
      </div>
    </div>

    <div class="login-container">
      <!-- 开场文字滚动效果 -->
      <div class="crawl-container" v-if="showCrawl">
        <div class="crawl">
          <div class="crawl-title">星际企业系统</div>
          <p class="crawl-text">
            很久很久以前，在一个遥远的星系...
          </p>
          <p class="crawl-text">
            企业资源规划系统横跨星际，连接无数文明，
            让商业贸易在银河系中畅通无阻。
          </p>
          <p class="crawl-text">
            现在，勇士们正在集结，准备进入这个宏大的系统，
            探索无限商机与挑战。
          </p>
        </div>
      </div>

      <!-- 登录面板 -->
      <div class="login-panel" v-show="!showCrawl">
        <div class="panel-header">
          <div class="logo-section">
            <div class="starwars-logo">
              <div class="logo-rebel"></div>
              <div class="logo-core"></div>
            </div>
            <h2 class="starwars-title">
              <span class="title-crawl">ERP</span>
              <span class="title-system">星际管理系统</span>
            </h2>
            <p class="starwars-subtitle">Welcome to the Galaxy</p>
          </div>
        </div>

        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <div class="input-wrapper">
              <el-input
                v-model="loginForm.username"
                placeholder="Pilot Name"
                size="large"
                prefix-icon="User"
                class="starwars-input"
              />
              <div class="input-beam"></div>
            </div>
          </el-form-item>

          <el-form-item prop="password">
            <div class="input-wrapper">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="Security Code"
                size="large"
                prefix-icon="Lock"
                class="starwars-input"
                show-password
              />
              <div class="input-beam"></div>
            </div>
          </el-form-item>

          <el-form-item>
            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe" class="starwars-checkbox">
                <span>保持连接</span>
              </el-checkbox>
              <a class="starwars-link" @click="forgotPassword">
                <span>失去连接?</span>
              </a>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="starwars-button"
              :loading="loading"
              @click="handleLogin"
            >
              <span class="button-text">进入系统</span>
              <span class="button-energy"></span>
            </el-button>
          </el-form-item>
        </el-form>

        <div class="panel-footer">
          <div class="footer-divider">
            <span class="divider-start">◄</span>
            <span class="divider-line"></span>
            <span class="divider-end">►</span>
          </div>
          <p class="footer-text">
            新飞行员?
            <a class="starwars-link" @click="register">
              <span>立即加入</span>
            </a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const store = useUserStore()

const emit = defineEmits(['login'])

const loginFormRef = ref(null)
const loading = ref(false)
const rememberMe = ref(false)
const showCrawl = ref(true)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await store.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '欢迎回来，指挥官')
      emit('login', response)
      router.push('/dashboard')
    } else {
      ElMessage.error(response.msg || '认证失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const register = () => {
  ElMessage.info('注册功能开发中...')
}

const forgotPassword = () => {
  ElMessage.info('密码重置功能开发中...')
}

onMounted(() => {
  // 开场动画结束后显示登录面板
  setTimeout(() => {
    showCrawl.value = false
  }, 8000)
})
</script>

<style scoped>
.starwars-login-wrap {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: radial-gradient(circle at center, #1a1a2e 0%, #000000 100%);
}

/* 太空背景 */
.space-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.stars-layer {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(2px 2px at 20% 30%, white, transparent),
                    radial-gradient(2px 2px at 60% 70%, rgba(255, 255, 255, 0.8), transparent),
                    radial-gradient(1px 1px at 50% 50%, white, transparent),
                    radial-gradient(1px 1px at 80% 10%, rgba(255, 255, 255, 0.6), transparent),
                    radial-gradient(2px 2px at 90% 80%, white, transparent);
  background-size: 200% 200%;
  animation: starMove 60s linear infinite;
}

.stars-layer-1 {
  animation-duration: 80s;
}

.stars-layer-2 {
  animation-duration: 100s;
  opacity: 0.7;
}

.stars-layer-3 {
  animation-duration: 120s;
  opacity: 0.5;
}

@keyframes starMove {
  0% { background-position: 0 0; }
  100% { background-position: 100% 100%; }
}

.star-field {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: 
    linear-gradient(transparent 0%, rgba(0, 255, 255, 0.1) 50%, transparent 100%),
    linear-gradient(90deg, transparent 0%, rgba(0, 255, 255, 0.1) 50%, transparent 100%);
  animation: starField 20s ease-in-out infinite;
}

@keyframes starField {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 0.7; }
}

.planets {
  position: absolute;
  width: 100%;
  height: 100%;
}

.planet {
  position: absolute;
  border-radius: 50%;
  box-shadow: 0 0 30px currentColor;
}

.planet-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  background: radial-gradient(circle at 30% 30%, #8b4513, #654321);
  color: #8b4513;
  animation: planetFloat 15s ease-in-out infinite;
}

.planet-2 {
  width: 150px;
  height: 150px;
  bottom: 15%;
  right: 15%;
  background: radial-gradient(circle at 30% 30%, #4169e1, #1e3a8a);
  color: #4169e1;
  animation: planetFloat 20s ease-in-out infinite 2s;
}

.planet-3 {
  width: 100px;
  height: 100px;
  top: 50%;
  right: 5%;
  background: radial-gradient(circle at 30% 30%, #ffd700, #ffa500);
  color: #ffd700;
  animation: planetFloat 12s ease-in-out infinite 4s;
}

@keyframes planetFloat {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(20px, -20px) scale(1.1); }
}

.ship-trails {
  position: absolute;
  width: 100%;
  height: 100%;
}

.trail {
  position: absolute;
  background: linear-gradient(
    to bottom,
    transparent,
    rgba(0, 255, 255, 0.6),
    transparent
  );
  width: 2px;
  animation: trailMove 8s linear infinite;
}

.trail-1 {
  left: 20%;
  animation-delay: 0s;
}

.trail-2 {
  left: 50%;
  animation-delay: 2.5s;
}

.trail-3 {
  left: 80%;
  animation-delay: 5s;
}

@keyframes trailMove {
  0% {
    height: 0;
    top: -50px;
    opacity: 0;
  }
  20% {
    opacity: 1;
  }
  80% {
    opacity: 1;
  }
  100% {
    height: 200px;
    top: 100%;
    opacity: 0;
  }
}

/* 登录容器 */
.login-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  perspective: 1200px;
}

/* 开场文字滚动 */
.crawl-container {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.crawl {
  color: #ffd700;
  font-family: 'Arial Black', sans-serif;
  font-size: 48px;
  text-align: center;
  animation: crawl 8s ease-in-out;
  text-shadow: 0 0 20px #ffd700;
}

.crawl-title {
  font-size: 64px;
  margin-bottom: 30px;
  text-transform: uppercase;
  letter-spacing: 10px;
}

.crawl-text {
  font-size: 24px;
  line-height: 2;
  margin-bottom: 40px;
  letter-spacing: 5px;
}

@keyframes crawl {
  0% {
    transform: translateY(100vh) scale(2);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100vh) scale(0.5);
    opacity: 0;
  }
}

/* 登录面板 */
.login-panel {
  position: relative;
  width: 480px;
  background: linear-gradient(
    135deg,
    rgba(0, 0, 0, 0.9),
    rgba(26, 26, 46, 0.8)
  );
  backdrop-filter: blur(10px);
  border: 2px solid rgba(255, 215, 0, 0.3);
  border-radius: 0;
  padding: 50px 40px;
  box-shadow: 
    0 0 40px rgba(255, 215, 0, 0.2),
    inset 0 0 40px rgba(255, 215, 0, 0.05);
  transform-style: preserve-3d;
  animation: panelAppear 1s ease-out;
}

@keyframes panelAppear {
  0% {
    opacity: 0;
    transform: rotateY(-90deg) translateZ(-100px);
  }
  100% {
    opacity: 1;
    transform: rotateY(0) translateZ(0);
  }
}

.panel-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-section {
  position: relative;
}

.starwars-logo {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto 25px;
}

.logo-rebel {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #ffd700, #ff8c00);
  clip-path: polygon(
    50% 10%,
    60% 30%,
    90% 30%,
    70% 50%,
    80% 70%,
    50% 60%,
    20% 70%,
    30% 50%,
    10% 30%,
    40% 30%
  );
  box-shadow: 0 0 30px #ffd700;
  animation: logoRotate 8s linear infinite;
}

.logo-core {
  position: absolute;
  width: 30px;
  height: 30px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle, #fff, #ffd700);
  border-radius: 50%;
  box-shadow: 0 0 20px #fff;
  z-index: 2;
}

@keyframes logoRotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.starwars-title {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 10px;
  letter-spacing: 3px;
}

.title-crawl {
  display: inline-block;
  color: #ffd700;
  text-shadow: 
    0 0 10px #ffd700,
    0 0 20px #ffd700,
    0 0 30px #ffd700;
  animation: titleGlow 2s ease-in-out infinite;
}

@keyframes titleGlow {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.title-system {
  display: block;
  font-size: 20px;
  color: #00ffff;
  text-shadow: 0 0 15px rgba(0, 255, 255, 0.5);
  margin-top: 5px;
}

.starwars-subtitle {
  font-size: 14px;
  color: #ffd700;
  margin: 0;
  letter-spacing: 4px;
  text-transform: uppercase;
}

/* 登录表单 */
.login-form {
  width: 100%;
}

.input-wrapper {
  position: relative;
  margin-bottom: 30px;
}

.starwars-input {
  width: 100%;
}

:deep(.el-input__wrapper) {
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(255, 215, 0, 0.3);
  box-shadow: none;
  transition: all 0.3s;
  border-radius: 0;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 215, 0, 0.6);
  box-shadow: 0 0 15px rgba(255, 215, 0, 0.3);
}

:deep(.el-input.is-focus .el-input__wrapper) {
  border-color: #ffd700;
  box-shadow: 0 0 20px rgba(255, 215, 0, 0.5);
  background: rgba(255, 215, 0, 0.1);
}

:deep(.el-input__inner) {
  color: #ffd700;
  background: transparent;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 215, 0, 0.5);
}

.input-beam {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(
    90deg,
    transparent,
    #ffd700,
    transparent
  );
  box-shadow: 0 0 10px #ffd700;
  transition: width 0.3s;
}

:deep(.el-input.is-focus ~ .input-beam) {
  width: 100%;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.starwars-checkbox :deep(.el-checkbox__label) {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.starwars-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #ffd700;
  border-color: #ffd700;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
}

.starwars-link {
  color: #00ffff;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
}

.starwars-link span {
  position: relative;
  z-index: 2;
}

.starwars-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 1px;
  background: #00ffff;
  box-shadow: 0 0 5px #00ffff;
  transition: width 0.3s;
}

.starwars-link:hover::after {
  width: 100%;
}

.starwars-link:hover {
  text-shadow: 0 0 10px #00ffff;
}

/* 登录按钮 */
.starwars-button {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, rgba(255, 215, 0, 0.3), rgba(255, 140, 0, 0.3));
  border: 2px solid #ffd700;
  color: #ffd700;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 3px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
  text-transform: uppercase;
  box-shadow: 
    0 0 20px rgba(255, 215, 0, 0.3),
    inset 0 0 20px rgba(255, 215, 0, 0.1);
}

.starwars-button:hover {
  background: linear-gradient(135deg, rgba(255, 215, 0, 0.6), rgba(255, 140, 0, 0.6));
  box-shadow: 
    0 0 30px rgba(255, 215, 0, 0.6),
    inset 0 0 30px rgba(255, 215, 0, 0.2);
  transform: translateY(-2px);
}

.button-text {
  position: relative;
  z-index: 2;
}

.button-energy {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 255, 255, 0.4),
    transparent
  );
  animation: buttonEnergy 2s linear infinite;
}

@keyframes buttonEnergy {
  0% { left: -100%; }
  100% { left: 100%; }
}

:deep(.starwars-button.is-loading) {
  pointer-events: none;
}

.panel-footer {
  text-align: center;
  margin-top: 30px;
}

.footer-divider {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 10px;
}

.divider-start,
.divider-end {
  color: #ffd700;
  font-size: 16px;
  text-shadow: 0 0 10px #ffd700;
}

.divider-line {
  flex: 1;
  height: 1px;
  background: linear-gradient(
    90deg,
    transparent,
    #ffd700,
    transparent
  );
  box-shadow: 0 0 5px #ffd700;
}

.footer-text {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}
</style>

